package com.cfs.config.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.TypeHandler;

/**
 * Created by Administrator on 2017/9/26/026.
 */
@MappedJdbcTypes({JdbcType.BIGINT})
public class DateTypeHandler implements TypeHandler<Date> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        preparedStatement.setTimestamp(i, new Timestamp(date.getTime()));
    }

    @Override
    public Date getResult(ResultSet resultSet, String s) throws SQLException {
        Timestamp timestamp = resultSet.getTimestamp(s);
        if(timestamp != null){
            return new Date(timestamp.getTime());
        }
        return null;
    }

    @Override
    public Date getResult(ResultSet resultSet, int i) throws SQLException {
        Timestamp timestamp = resultSet.getTimestamp(i);
        if(timestamp != null){
            return new Date(timestamp.getTime());
        }
        return null;
    }

    @Override
    public Date getResult(CallableStatement callableStatement, int i) throws SQLException {
        Timestamp timestamp = callableStatement.getTimestamp(i);
        if(timestamp != null){
            return new Date(timestamp.getTime());
        }
        return null;
    }
}
